#include "memory.hpp"
#include "command.hpp"

#include <cassert>
#include <string>
#include <sstream>
#include <iostream>
#include <cstdint>
#include <map>
#include <cstdio>

using namespace std;

int analizeCommand(uint16_t a)
{
	auto x = CommandMap.begin();
	for(; x != CommandMap.end(); x++)
	{
		if((GM.rw(a) & x->second.mask) == x->second.opcode)
			return x->second.opcode;
	}
	assert(0);
}

int executeCommand(uint16_t a)
{
	uint16_t opcode = analizeCommand(a);
	GM.ww(PC,a+2);
	CommandMap[opcode].func(CommandMap[opcode].dataRead(a));
	return GM.rw(PC);
}

int printCommand(uint16_t a)
{
	uint16_t opcode = analizeCommand(a);
	GM.ww(PC,a+2);
	stringstream ss;
	ss << oct << GM.rw(a);
	cout << ss.str() << " == " << CommandMap[opcode].name << CommandMap[opcode].dataPrettifier(CommandMap[opcode].dataRead(a)) << "\n";
	return GM.rw(PC);
}

//TODO: return type
void registerCommand(Command c)
{
	cout << CommandMap.insert(pair <uint16_t, Command> (c.opcode,c)).second;
}

void registerAllComands();